var app = getApp(), util = require("../../utils/util.js"),WxParse = require("../wxParse/wxParse.js");

Page({
    data: {
        f_id: "",
        openid: "",
        userAge: "",
        index: 0,
        toggle: !0,
        datalist:0,
        dateArray:[],
        objArray:{},
        dtArray:[],
        widths: 300,
        heigths: 400,
        modalShow:false,
        baoming_yajin:0,
        baoming_content:'',
        user_account:'',
        is_hy:'',
        u_id:'',
        useMoney:'',
        jztype:''
    },
    onLoad: function(a) {
        var n = this;
        var e = a.f_id,datelist=a.datalist,jztype=a.jzType;
        n.setData({
            f_id: e,
            datalist:datelist,
            jztype:jztype,
            openid: wx.getStorageSync("openid")
        });
         n.getUsers();
        app.util.request({
            url: "entry/wxapp/User",
            data: {
                openid: wx.getStorageSync("openid")
            },
            success: function(a) {
                var e = a.data.data.u_chusheng, t = new Date().getFullYear() - e.split("-")[0];
                n.setData({
                    user: a.data.data,
                    userAge: t
                });
            }
        });
        app.util.request({
            url: "entry/wxapp/Jianzhi_baoming_yajin",
            data: {
                type: n.data.jztype
            },
            success: function(a) {
               n.setData({
                    baoming_yajin: a.data.data,
                });
            }
        });
        app.util.request({
            url: "entry/wxapp/base",
            success: function(a) {
                WxParse.wxParse("article", "html", a.data.data.xuzhi,n, 5);
            }
        });
        // 多日订单日期选择
        // if(n.data.datalist != 0){
        //        n.choseDate();
        //        setTimeout(function(){
        //         n.data.dateArray[0].checked = true;
        //         n.setData({
        //          dateArray: n.data.dateArray,
        //        });
        //         n.pushDateList();
        //    },300)
        // }else{
        //     return;
        // };
       
    },
     getUsers: function() {
        var t = this;
        app.util.request({
            url: "entry/wxapp/User",
            data: {
                openid: wx.getStorageSync("openid")
            },
            success: function(a) {
                t.setData({
                    is_hy: a.data.data.u_hy,
                    u_id: a.data.data.u_id,
                    user_account: a.data.data.u_money
                })
            }
        });
    },
  radioButtonTap:function(e){
    console.log(e);
    var self = this;
    let id = e.currentTarget.dataset.id
    console.log(id)
    for (let i = 0; i < self.data.dateArray.length; i++) {
      if (self.data.dateArray[i].f_id == id) {
        if (self.data.dateArray[i].checked == true) {
          self.data.dateArray[i].checked = false;
        } else {
          self.data.dateArray[i].checked = true;
        }
      }
    };
    self.setData({
     dateArray: self.data.dateArray
    });
    setTimeout(function(){
        self.pushDateList();
    },300)
  },
  pushDateList:function(){
    var self = this;
    var dateObj = {};
    let objList = '';
    // for(var i = 0 ;i < self.data.dateArray.length; i++){
    //     if(self.data.dateArray[i].checked == true){
    //         objList = self.data.dateArray[i].f_id;
    //         dateObj[i] = objList;
    //     };
    // };
    // self.data.dtArray = dateObj;
    console.log(self.data.dtArray);
  },
    inputClick: function(a) {
        var e = a.detail.cursor;
        this.setData({
            index: e
        });
    },
    choseDate:function(){
        var self = this;
        app.util.request({
            url: "entry/wxapp/Datelist",
            data: {
                f_id: self.data.f_id
            },
            success: function(a) {
               self.setData({
                dateArray:a.data.data
               })

            }
        })
    },
    sendClick:function(){
        var self = this;
        self.setData({
                   modalShow:false,
                   toggle: !0
                  }); 
        if(self.data.jztype == '朋友圈集赞' || self.data.jztype == '活动转发'){
               console.log("朋友圈集赞");
               app.util.request({
                       url: "entry/wxapp/Baoming",
                       data: {
                              e:self.data.baoming_content,
                              f_id: self.data.f_id
                              },
                            success: function(a) {
                                wx.showToast({
                                    title: "报名成功!",
                                    success: function(a) {
                                             self.setClock();
                                            }
                                        });
                                    }
                        });
               
            
          }else{
          var payMoney=(Number(self.data.user_account) - Number(self.data.baoming_yajin));
          if(payMoney >= 0){
           self.setData({
            useMoney:Number(self.data.baoming_yajin)
           })
            app.util.request({
            url: "entry/wxapp/User_yue",
            data: {
               openid: wx.getStorageSync("openid"),
               money:Number(self.data.baoming_yajin),
               f_id: self.data.f_id
            },
            success: function(a) {
               app.util.request({
                       url: "entry/wxapp/Baoming",
                       data: {
                              e:self.data.baoming_content,
                              f_id: self.data.f_id,
                              baoming_yajin:Number(self.data.baoming_yajin),
                              fid_list:self.data.dtArray
                              },
                            success: function(a) {
                                wx.showToast({
                                    title: "报名成功!",
                                    success: function(a) {
                                       self.setData({
                                              modalShow:false,
                                              toggle: !0
                                              }); 
                                        self.setClock();
                                        setTimeout(function() {
                                           wx.reLaunch({
                                              url: "/hyb_jianzhi/success/success"
                                           });
                                            }, 1e3);
                                            }
                                        });
                                    }
                        });
            }
          })
          }else{
            self.setData({
                     modalShow:false,
                     toggle: !0,
                     useMoney:self.data.user_account
                }); 
            var payWx = (Number(self.data.baoming_yajin)-Number(self.data.user_account)).toFixed(2);
                  app.util.request({
                        url: "entry/wxapp/Pay",
                            data: {
                                 openid: wx.getStorageSync("openid"),
                                 zhiding_money: payWx
                               },
                             header: {
                              "Content-Type": "application/json"
                            },
                            success: function(a) {
                               wx.requestPayment({
                               timeStamp: a.data.timeStamp,
                               nonceStr: a.data.nonceStr,
                               package: a.data.package,
                               signType: a.data.signType,
                               paySign: a.data.paySign,
                               success: function(a) {
                                util.formatTime(new Date());
                                 app.util.request({
                                   url: "entry/wxapp/User_yue",
                                    data: {
                                       openid: wx.getStorageSync("openid"),
                                       money:self.data.user_account
                                   },
                                  success: function(a) {
                                     app.util.request({
                                      url: "entry/wxapp/Baoming",
                                      data: {
                                        e:self.data.baoming_content,
                                        f_id: self.data.f_id,
                                        baoming_yajin:Number(self.data.baoming_yajin),
                                        fid_list:self.data.dtArray
                                     },
                                    success: function(a) {
                                         wx.showToast({
                                            title: "报名成功!",
                                            success: function(a) {
                                                setTimeout(function() {
                                                    wx.reLaunch({
                                                        url: "/hyb_jianzhi/success/success"
                                                    });
                                                }, 1e3);
                                            }
                                        });
                                    }
                                });
                                }
                             })
                            }
                        });
                    }
                  })
              }
          }
    },
    setClock:function(){
    let that = this;
    if (wx.requestSubscribeMessage) {
      wx.requestSubscribeMessage({
        tmplIds: ['Ujzc1t5qLe1oV_tpVuj3prenIdMonbdI7r9oHD23jTM','Ujzc1t5qLe1oV_tpVuj3prenIdMonbdI7r9oHD23jTM','Ujzc1t5qLe1oV_tpVuj3prenIdMonbdI7r9oHD23jTM'],
        success(res) {
          if (res['Ujzc1t5qLe1oV_tpVuj3prenIdMonbdI7r9oHD23jTM','Ujzc1t5qLe1oV_tpVuj3prenIdMonbdI7r9oHD23jTM','Ujzc1t5qLe1oV_tpVuj3prenIdMonbdI7r9oHD23jTM'] === 'accept') {
          } else if (res['Ujzc1t5qLe1oV_tpVuj3prenIdMonbdI7r9oHD23jTM','Ujzc1t5qLe1oV_tpVuj3prenIdMonbdI7r9oHD23jTM','Ujzc1t5qLe1oV_tpVuj3prenIdMonbdI7r9oHD23jTM'] === 'reject') {
            that.guideOpenSubscribeMessage();
          } else {
            wx.showToast({
              title: '授权订阅消息有误',
              icon: 'none'
            });
          }
        },
        fail(res) {
          // 20004:用户关闭了主开关，无法进行订阅,引导开启
          if (res.errCode == 20004) {
            // console.log(res, 'fail:用户关闭了主开关，无法进行订阅,引导开启---');
            that.guideOpenSubscribeMessage();
          }
        }
      });
    } else {
      wx.showToast({
        title: '请更新您微信版本，来获取订阅消息功能',
        icon: 'none'
      });
    }
  },
    subClick: function(a) {
        var e = a.detail.value,self = this,arr = Object.keys(self.data.dtArray);
        self.setData({
            baoming_content:e,
            toggle: !1
        });
        // console.log(arr.length)
        // if(arr.length == 0 && self.data.datalist!=0){
        //     wx.showToast({
        //     title: "请选择报名日期!",
        //     image: "/hyb_jianzhi/images/errors.png"
        //     }) 
        //     return;
        // };
          app.util.request({
            url: "entry/wxapp/Userbaiming",
            data: {
                openid: wx.getStorageSync("openid"),
                f_id: this.data.f_id
            },
            success: function(a) {
                if ("false" != a.data.data.baoming || "0" != self.data.baoming_money && "" != self.data.baoming_money ) {
                    if("false" != a.data.data.baoming || "0" == self.data.baoming_money  && "" == self.data.baoming_money ){
                        if( "true" == a.data.data.baoming){
                            wx.showToast({
                            title: "您已报名",
                          image: "/hyb_jianzhi/images/errors.png",
                          success: function() {
                              setTimeout(function(){
                                 wx.navigateTo({
                                url: "/hyb_jianzhi/index/index"
                               });
                                },500)
                              }
                          })
                        } 
                    }else{
                      self.setData({
                          modalShow:true
                       }) 
                    } 
                } else {
                    util.formatTime(new Date());
                    app.util.request({
                        url: "entry/wxapp/Baoming",
                        data: {
                                e:e,
                                f_id: self.data.f_id,
                                fid_list:self.data.dtArray
                              },
                        success: function(a) {
                            console.log(a), wx.showToast({
                                title: "报名成功!",
                                success: function(a) {
                                    console.log(a), setTimeout(function() {
                                        wx.reLaunch({
                                            url: "/hyb_jianzhi/success/success"
                                        });
                                    }, 1e3);
                                }
                            });
                        }
                    });
                }
            }
        });
    },
     closemodal: function(t) {
        this.setData({
            modalShow: false,
            toggle: !0
        });
    },
    onShareAppMessage: function() {}
});